<?php
require_once dirname(__FILE__) . '/../includes/common.inc.php';
require_once dirname(__FILE__) . '/../includes/pdoorm.inc.php';

  $orderid = MiscUtils::getParam('cno', '');
  
   $tmpaf = array();

   if(!empty($orderid))
   {
   	  $sql = "select * from ns_v1customer where e_oid =".$orderid." ";
   	  
   	  $cusinfo = getone($sql);
   	  
   	  $tmpaforders = array();
   	  
   	  $tmporder = getsonall('ns_v1myorder','customerId',$orderid);
   	  
      foreach($tmporder as $order)
   	  {
   	      $order->products = array();
          $pitem = getsonall('ns_v1purchaseitem','order_id',$order->e_oid);
          
          $order->products = $pitem;
          
          $order->payments = array();
          $payment = getsonall('ns_v1payment','orderId',$order->e_oid); 
          $order->payments = $payment;
          
          $order->contract = array();
          
          $contract = getsonall('ns_contract','orderType',$order->type);
          $order->contract = $contract;          
          
          $tmpaforders[] = $order;
   	  }
   	  
   	  
   	  $tmpaf['bridename'] = $cusinfo->bridename;
   	  $tmpaf['bridephone'] = $cusinfo->bridephone;
   	  $tmpaf['bridemobile'] = $cusinfo->bridemobile;
   	  $tmpaf['brideemail'] = $cusinfo->brideemail;
   	  $tmpaf['brideaddrLn1'] = $cusinfo->brideaddrLn1;
   	  $tmpaf['brideaddrLn2'] = $cusinfo->brideaddrLn2;
   	  $tmpaf['brideaddrLn3'] = $cusinfo->brideaddrLn3;
   	  $tmpaf['brideaddrLn4'] = $cusinfo->brideaddrLn4;
   	  
   	  $tmpaf['groomname'] = $cusinfo->groomname;
   	  $tmpaf['groomphone'] = $cusinfo->groomphone;
   	  $tmpaf['groommobile'] = $cusinfo->groommobile;
   	  $tmpaf['groomemail'] = $cusinfo->groomemail;
   	  $tmpaf['groomaddrLn1'] = $cusinfo->groomaddrLn1;
   	  $tmpaf['groomaddrLn2'] = $cusinfo->groomaddrLn2;
   	  $tmpaf['groomaddrLn3'] = $cusinfo->groomaddrLn3;
   	  $tmpaf['groomaddrLn4'] = $cusinfo->groomaddrLn4;
   	  $tmpaf['customerid'] = $cusinfo->e_oid;
   	  
   	  $tmpaf['orders'] = $tmpaforders;
   	  
   	  
   } 

$html ="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
$html .="<html>";
$html .=" <head>";
$html .=" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />";
$html .="  <title>Bookform</title>";
$html .=" </head>";
$html .=" <body >";
$html .="<div id=\"container\">";
$html .="  <table class=\"contract_table1\" cellPadding=\"0\" cellSpacing=\"0\">";
$html .="    <tr>";
$html .="      <td class=\"contract_td1\">";
$html .="        Customer No.";
$html .="      </td>";
   $cusno = BRANCH.customerno($_REQUEST['cno']); 
$html .="      <td class=\"contract_td2\" id=\"customerno\">".$cusno."</td>";
$html .="    </tr>";
$html .="  </table>";
  
 $html .=" <table class=\"contract_table2\" cellPadding=\"0\" cellSpacing=\"0\">";
$html .="    <tr>";
 $html .="     <td>";
 $html .="       <img src=\"../images/common/line.png\"></img>";
$html .="      </td>";
 $html .="   </tr>";
 $html .=" </table>";
  
 $html .=" <table class=\"contract_table3\" cellPadding=\"0\" cellSpacing=\"0\">";
$html .="    <tr>";
$html .="<td class=\"contract_td3\">";
$html .="        Bride Name";
      $html .="</td>";
      $brindename = ($tmpaf['bridename']) ? $tmpaf['bridename'] : '';
$html .=" <td class=\"contract_td4\" id=\"bridename\">".$brindename."</td>";
$html .="      <td class=\"contract_td5\">";
$html .="        Groom Name";
$html .="      </td>";
$groomname = ($tmpaf['groomname']) ? $tmpaf['groomname'] : '';
$html .="      <td class=\"contract_td6\" id=\"groomname\">".$groomname."</td>";
$html .="    </tr>";
    
$html .="    <tr>";
$html .="      <td class=\"contract_td3\">";
$html .="       Address";
 $html .="     </td>";
 $bridead1 = ($tmpaf['brideaddrLn1']) ? $tmpaf['brideaddrLn1'] : '';
$html .="      <td class=\"contract_td4\" id=\"bridead1\">".$bridead1."</td>";
 $html .="     <td class=\"contract_td5\">";
$html .="       Address";
$html .="      </td>";
       $groomad1 = ($tmpaf['groomaddrLn1']) ? $tmpaf['groomaddrLn1'] : '';
$html .="      <td class=\"contract_td6\" id=\"groomad1\">".$groomad1."</td>";
 $html .="   </tr>";
    
$html .="    <tr>";
$html .="      <td class=\"contract_td3\">&nbsp;</td>";
$bridead2 = ($tmpaf['brideaddrLn2']) ? $tmpaf['brideaddrLn2'] : '';
$html .="      <td class=\"contract_td4\" id=\"bridead2\">".$bridead2."</td>";
 $html .="     <td class=\"contract_td5\">&nbsp;</td>";
 $groomad2 = ($tmpaf['groomaddrLn2']) ? $tmpaf['groomaddrLn2'] : '';
$html .="      <td class=\"contract_td6\" id=\"groomad2\">".$groomad2."</td>";
$html .="    </tr>";

$html .="    <tr>";
$html .="      <td class=\"contract_td3\">";
 $html .="       Phone";
$html .="     </td>";
$bridephone = ($tmpaf['bridephone']) ? $tmpaf['bridephone'] : '';
 $html .="     <td class=\"contract_td4\" id=\"bridephone\">".$bridephone."</td>";
$html .="      <td class=\"contract_td5\">";
 $html .="       Phone";
$html .="      </td>";
$groomphone = ($tmpaf['groomphone']) ? $tmpaf['groomphone'] : '';
$html .="      <td class=\"contract_td6\" id=\"groomphone\">".$groomphone."</td>";
 $html .="   </tr>";
    
$html .="    <tr>";
 $html .="     <td class=\"contract_td3\">";
 $html .="       Mobile";
 $html .="     </td>";
 $bridemobile = ($tmpaf['bridemobile']) ? $tmpaf['bridemobile'] : '';
 $html .="     <td class=\"contract_td4\" id=\"bridemobile\">".$bridemobile."</td>";
 $html .="     <td class=\"contract_td5\">";
 $html .="       Mobile";
$html .="      </td>";
$groommobile = ($tmpaf['groommobile']) ? $tmpaf['groommobile'] : '';
 $html .="     <td class=\"contract_td6\" id=\"groommobile\">".$groommobile."</td>";
 $html .="   </tr>";
$html .="    <tr>";
$html .="      <td class=\"contract_td3\">";
$html .="        Email";
$html .="      </td>";
$brideemail = ($tmpaf['brideemail']) ? $tmpaf['brideemail'] : '';
$html .="      <td class=\"contract_td4\" id=\"brideemail\">".$brideemail."</td>";
$html .="      <td class=\"contract_td5\">";
$html .="        Email";
$html .="      </td>";
$groomemail = ($tmpaf['groomemail']) ? $tmpaf['groomemail'] : '';
$html .="      <td class=\"contract_td6\" id=\"groomemail\">".$groomemail."</td>";
$html .="    </tr>";
$html .="  </table>";
  
$html .="  <table class=\"contract_table2\" cellPadding=\"0\" cellSpacing=\"0\">";
$html .="    <tr>";
$html .="      <td>";
$html .="        <img src=\"../images/common/line.png\"></img>";
$html .="      </td>";
$html .="    </tr>";
$html .="  </table>";

$orders = $tmpaf['orders'];

if(!empty($orders)){
	
	  foreach($orders as $onp){
	  	
	        $html .="  <table class=\"contract_table9\" cellPadding=\"0\" cellSpacing=\"0\">";
			$html .="    <tr>";
			$html .="      <td id=\"contract_orderInfo_div\" align=\"center\">";
			$html .="      <table width=\"100%\" class=\"pl15\" border=0>";
			$html .="         <tr>";
			$html .="            <td id='otype' class=\"contract_td3  f140\">".$onp->type."</td>";
			$html .="            <td  colspan='3'><div id='odate' >".SimpleDate::toUTC($onp->date)."</div></td>";
			$html .="         </tr>";
			
			$html .="         <tr style=\"height:30px;\">";
			$html .="            <td class=\"contract_td3 f120\" nowrap width=\"25%\">Photo Package</td>";
			$html .="            <td  width=\"30%\" class=\"contract_td4\"><div id='ptype'>".$onp->photoname."</div></td>";
			$html .="            <td class=\"contract_td5 f120\" nowrap width=\"20%\">Video Package</td>";
			$html .="            <td   width=\"25%\" class=\"contract_td6\"><div id='vtype'>".$onp->videoname."</div></td>";
			$html .="         </tr>";

			
					$html .="        <!-- package includes -->";
					$html .="         <tr>";
					$html .="            <td class=\"contract_td5 f130\" colspan='4'>Package Includes</td>";
					$html .="         </tr>";
					$html .="         <tr>";
					$html .="            <td colspan='4'>";
					$html .="               <table   class=\"tltable\" cellSpacing = '0' cellpadding = '0'>";
					$html .="                  <tr>";
					$html .="                      <td class='contract_td5 tltd td1  f120'>Name</td>";
					$html .="                      <td class='contract_td5 tltd td23 f120'>Qty</td>";
					$html .="                      <td class='contract_td5 tltd td23 f120'>Unit</td>";
					$html .="                  </tr>";
			
			        $products = $onp->products;
			        
			 if(!empty($products)){       
			      foreach($products as $onpro){
			      	if($onpro->cancelled == 1) continue;
			      	if($onpro->packageInc == '') continue;
			      	
					$html .="        <!-- package includes -->";
					$html .="                  <tr>";
					$html .="                      <td class='contract_td5 tltd td1  f120'>$onpro->name</td>";
					$html .="                      <td class='contract_td5 tltd td23 f120'>$onpro->quantity</td>";
					$html .="                      <td class='contract_td5 tltd td23 f120'>$onpro->unit</td>";
					$html .="                  </tr>";

					$html .="        <!-- package includes -->";
			      }
			 }else{
			 	
					$html .="                  <tr>";
					$html .="                      <td colspan='3' class='contract_td5 tltd td1  f120'>N/A</td>";
					$html .="                  </tr>";         
	 	
			 }
			 
					$html .="               </table>";
					$html .="            </td>";
					$html .="         </tr>";
					$html .="        <!-- package includes -->";		
			        
			        
			        
			$html .="         <!-- Additions -->";
			$html .="         <tr>";
			$html .="            <td class=\"contract_td5  f130\" colspan='4'>Additions</td>";
			$html .="         </tr>";
			$html .="         <tr>";
			$html .="            <td colspan='4'>";
			$html .="               <table class=\"tltable\"  cellSpacing = '0' cellpadding = '0'>";
			$html .="                  <tr>";
			$html .="                      <td class='contract_td5 tltd td1 f120'>Name</td>";
			$html .="                      <td class='contract_td5 tltd td23 f120'>Qty</td>";
			$html .="                      <td class='contract_td5 tltd td23 f120'>Unit</td>";
			$html .="                  </tr>";
			
			                    if(!empty($products)){
			                    	foreach($products as $aon){
			                    		if($aon->cancelled == 1) continue;
			      	                    if($aon->packageInc != '') continue;
			      	                    
									$html .="        <!-- package includes -->";
									$html .="                  <tr>";
									$html .="                      <td class='contract_td5 tltd td1  f120'>$aon->name</td>";
									$html .="                      <td class='contract_td5 tltd td23 f120'>$aon->quantity</td>";
									$html .="                      <td class='contract_td5 tltd td23 f120'>$aon->unit</td>";
									$html .="                  </tr>";
				
									$html .="        <!-- package includes -->";			                    		
			                    	}
			                    }else{
			                    	
									$html .="                  <tr>";
									$html .="                      <td colspan='3' class='contract_td5 tltd td1  f120'>N/A</td>";
									$html .="                  </tr>";     			                    	
			                    }
			
               
			$html .="               </table>";
			$html .="            </td>";
			$html .="         </tr>";
			$html .="        <!-- Additions -->       ";
	  
			$html .="          <!-- payment -->";
			$html .="         <tr>";
			$html .="            <td class=\"contract_td5  f130\" colspan='4'>Payment</td>";
			$html .="         </tr>";
			$html .="         <tr style=\"heigth:30px;\">";
			$html .="            <td class=\"contract_td5\">Price</td>";
			$price = $onp->price > 0 ? sprintf("%.2f",$onp->price*(1+ GST)).'(Inc.'. TAX .')' : '$0.00';
			$html .="            <td class=\"bder\" colspan='3' id=\"price\">".$price."</td>";
			$html .="         </tr>";
			$html .="         <tbody id='showpayment'>";
			
	      	 $rule =  payment($onp->type);

    	 for($i = 1, $j = count($rule); $i <= $j;$i++)
    	 {
    	 	$ru = $rule[$i-1];
    	 	
    	 	if($i == 1){
    	 		  $ptitle = 'Deposit';
    	 	}else{
    	 		if($i == 2) $pt = 'nd';
    	 		if($i == 3) $pt = 'rd';
    	 		if($i == 4) $pt = 'th';
    	 		
    	 		$ptitle = $i."<sup>$pt</sup>" .'  payment';
    	 	}
    	 	
    	 	//$price1 = sprintf("%.2f",$onp->price*(1+ GST)).'(Inc.'. TAX .')';
    	 	$price1 = sprintf("%.2f",$onp->price*(1+ GST));
    	 	$tpr = sprintf("%.2f",$price1* $ru['perc']).'(Inc.'. TAX .')  '.strtolower($ru['comment']);
    	 	
    	 $html .="         <tr>";
    	 $html .="         <td class='contract_td5'>".$ptitle."</td>";
    	 $html .="         <td colspan = '3'>".$tpr."</td>";
    	 $html .="         </tr>";    	 	
 		       		 
    	 }			
    	 

			
			$html .="         </tbody>";
			$html .="        <!-- payment -->           ";
			        
			$html .="         <!-- note --> ";
			$html .="          <tr>";
			$html .="            <td class=\"contract_td5  f130\">Note</td>";
			$html .="            <td  colspan='3' id=\"cnote\">".$onp->note."</td>";
			$html .="         </tr>        ";
			$html .="         <!-- note --> ";
			$html .="      </table>";
			        
			$html .="      </td>";
			$html .="    </tr>";
			$html .="  </table>";	  
	  }
	
}else{
			$html .="  <table class=\"contract_table9\" cellPadding=\"0\" cellSpacing=\"0\">";
			$html .="    <tr>";
			$html .="      <td id=\"contract_orderInfo_div\" align=\"center\">";
			$html .="      <table width=\"100%\" class=\"pl15\" border=0>";
			$html .="         <tr>";
			$html .="            <td id='otype' class=\"contract_td3  f140\"></td>";
			$html .="            <td  colspan='3'><div id='odate' ></div></td>";
			$html .="         </tr>";
			
			$html .="         <tr style=\"height:30px;\">";
			$html .="            <td class=\"contract_td3 f120\" nowrap width=\"25%\">Photo Package</td>";
			$html .="            <td  width=\"30%\" class=\"contract_td4\"><div id='ptype'></div></td>";
			$html .="            <td class=\"contract_td5 f120\" nowrap width=\"20%\">Video Package</td>";
			$html .="            <td   width=\"25%\" class=\"contract_td6\"><div id='vtype'></div></td>";
			$html .="         </tr>";
			$html .="        <!-- package includes -->";
			$html .="         <tr>";
			$html .="            <td class=\"contract_td5 f130\" colspan='4'>Package Includes</td>";
			$html .="         </tr>";
			$html .="         <tr>";
			$html .="            <td colspan='4'>";
			$html .="               <table   class=\"tltable\" cellSpacing = '0' cellpadding = '0'>";
			$html .="                  <tr>";
			$html .="                      <td class='contract_td5 tltd td1  f120'>Name</td>";
			$html .="                      <td class='contract_td5 tltd td23 f120'>Qty</td>";
			$html .="                      <td class='contract_td5 tltd td23 f120'>Unit</td>";
			$html .="                  </tr>";
			$html .="                     <tbody id='pproducts'>";
			$html .="                     </tbody>      ";            
			$html .="               </table>";
			$html .="            </td>";
			$html .="         </tr>";
			$html .="        <!-- package includes -->";
			        
			        
			        
			$html .="         <!-- Additions -->";
			$html .="         <tr>";
			$html .="            <td class=\"contract_td5  f130\" colspan='4'>Additions</td>";
			$html .="         </tr>";
			$html .="         <tr>";
			$html .="            <td colspan='4'>";
			$html .="               <table class=\"tltable\"  cellSpacing = '0' cellpadding = '0'>";
			$html .="                  <tr>";
			$html .="                      <td class='contract_td5 tltd td1 f120'>Name</td>";
			$html .="                      <td class='contract_td5 tltd td23 f120'>Qty</td>";
			$html .="                      <td class='contract_td5 tltd td23 f120'>Unit</td>";
			$html .="                  </tr>";
			$html .="                     <tbody id='aproducts'>";
			$html .="                     </tbody>        ";                
			$html .="               </table>";
			$html .="            </td>";
			$html .="         </tr>";
			$html .="        <!-- Additions -->       ";


        
        
			$html .="          <!-- payment -->";
			$html .="         <tr>";
			$html .="            <td class=\"contract_td5  f130\" colspan='4'>Payment</td>";
			$html .="         </tr>";
			$html .="         <tr style=\"heigth:30px;\">";
			$html .="            <td class=\"contract_td5\">Price</td>";
			$html .="            <td class=\"bder\" colspan='3' id=\"price\"></td>";
			$html .="         </tr>";
			$html .="         <tbody id='showpayment'>";
			$html .="         </tbody>";
			$html .="        <!-- payment -->           ";
			        
			$html .="         <!-- note --> ";
			$html .="          <tr>";
			$html .="            <td class=\"contract_td5  f130\">Note</td>";
			$html .="            <td  colspan='3' id=\"cnote\"></td>";
			$html .="         </tr>        ";
			$html .="         <!-- note --> ";
			$html .="      </table>";
			        
			$html .="      </td>";
			$html .="    </tr>";
			$html .="  </table>";
}
  
$html .="  <table class=\"contract_table2\" cellPadding=\"0\" cellSpacing=\"0\">";
$html .="    <tr>";
$html .="      <td>";
$html .="        <img src=\"../images/common/line.png\"></img>";
$html .="      </td>";
$html .="    </tr>";
$html .="  </table>";
  
if(!empty($orders))
{  $contract = $orders[0]->contract[0];
	$html .="  <table class=\"contract_table8\" cellPadding=\"0\" cellSpacing=\"0\">";
	$html .="    <tr>";
	$html .="      <td class=\"contract_td24\" id=\"contract_list_div\">".nl2br($contract->content)."</td>";
	$html .="    </tr>";
	$html .="  </table>";
}else{
	$html .="  <table class=\"contract_table8\" cellPadding=\"0\" cellSpacing=\"0\">";
	$html .="    <tr>";
	$html .="      <td class=\"contract_td24\" id=\"contract_list_div\"></td>";
	$html .="    </tr>";
	$html .="  </table>";	
}

  $html .="  <table class=\"contract_table8\" cellPadding=\"0\" cellSpacing=\"0\">";
  $html .="    <tr>";
   $html .="     <td class=\"cc_td25\">Signature _____________________</td>";
   $html .="     <td class=\"cc_td26\">Date ___________________</td>";
   $html .="   </tr>";
 $html .="   </table>";

$html .="</div>";
$html .=" </body>";
$html .="</html>";

//==============================================================
//==============================================================
//==============================================================


include dirname(__FILE__)."/../library/MPDF56/mpdf.php";


$dest_dir = dirname(__FILE__)."/files/".$_REQUEST['cno']; 
if(!is_dir($dest_dir)) mkdir($dest_dir,0777); 
$filename = 'bookform'.date("YmdHis").'-'.$_REQUEST['cno'].'.pdf';
$aa=$dest_dir.'/'.$filename;

$mpdf=new mPDF(); 

// LOAD a stylesheet
$stylesheet = file_get_contents('bookform.css');
$mpdf->WriteHTML($stylesheet,1);	// The parameter 1 tells that this is css/style only and no body/html/text

$mpdf->WriteHTML($html);


$mpdf->Output($aa);
echo $filename;
exit;

//==============================================================
//==============================================================
//==============================================================


?>